﻿namespace UnityEditor
{
    using System;
    using System.Collections;
    using System.IO;
    using UnityEditorInternal;
    using UnityEngine;

    internal class PackageImport : EditorWindow
    {
        [SerializeField]
        private AssetsItem[] m_assets;
        private string m_LastPreviewPath;
        [SerializeField]
        private int m_LeastIndent = 0xf423f;
        [SerializeField]
        private ListViewState m_ListView = new ListViewState(0, 0x12);
        [SerializeField]
        private bool m_PackageContainsPreviews;
        [SerializeField]
        private string m_PackageIconPath;
        private Texture2D m_Preview;
        private static Constants ms_Constants;

        public PackageImport()
        {
            base.minSize = new Vector2(460f, 350f);
            this.m_PackageContainsPreviews = false;
        }

        private void CheckChildren(AssetsItem parentAI)
        {
            foreach (AssetsItem item in this.m_assets)
            {
                if (item.parentGuid == parentAI.guid)
                {
                    item.enabled = parentAI.enabled;
                    this.CheckChildren(item);
                }
            }
        }

        private static int CountOccurencesOfChar(string instance, char c)
        {
            int num = 0;
            foreach (char ch in instance)
            {
                if (c == ch)
                {
                    num++;
                }
            }
            return num;
        }

        private void FrameLastGUIRect()
        {
            Rect lastRect = GUILayoutUtility.GetLastRect();
            HandleUtility.handleWireMaterial.SetPass(0);
            GL.Begin(1);
            GL.Color(ms_Constants.lineColor);
            GL.Vertex3(lastRect.xMax + 1f, lastRect.y, 0f);
            GL.Vertex3(lastRect.xMax + 1f, lastRect.yMax, 0f);
            GL.Vertex3(lastRect.xMax + 1f, lastRect.yMax, 0f);
            GL.Vertex3(lastRect.x + 1f, lastRect.yMax, 0f);
            GL.Vertex3(lastRect.x + 1f, lastRect.yMax, 0f);
            GL.Vertex3(lastRect.x + 1f, lastRect.y, 0f);
            GL.End();
        }

        private Texture2D GetIcon(AssetsItem ai)
        {
            if (ai.assetIsDir != 0)
            {
                return EditorGUIUtility.FindTexture(EditorResourcesUtility.folderIconName);
            }
            return InternalEditorUtility.GetIconForFile(ai.pathName);
        }

        private void OnDisable()
        {
            if (this.m_Preview != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_Preview);
            }
        }

        public void OnGUI()
        {
            if (ms_Constants == null)
            {
                ms_Constants = new Constants();
            }
            if (this.m_assets != null)
            {
                if (this.m_LeastIndent == 0xf423f)
                {
                    int leastIndent = this.m_LeastIndent;
                    for (int i = 0; i < this.m_assets.Length; i++)
                    {
                        int num3 = CountOccurencesOfChar(this.m_assets[i].pathName, '/');
                        if (leastIndent > num3)
                        {
                            leastIndent = num3;
                        }
                    }
                    this.m_LeastIndent = leastIndent - 1;
                }
                if (this.m_assets.Length > 0)
                {
                    this.m_ListView.totalRows = this.m_assets.Length;
                    if (this.m_PackageContainsPreviews)
                    {
                        GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                        GUILayout.Space(2f);
                    }
                    GUILayout.BeginVertical(new GUILayoutOption[0]);
                    GUILayout.Label("Items to Import", ms_Constants.title, new GUILayoutOption[0]);
                    GUILayout.Space(1f);
                    bool flag = Event.current.type == EventType.Repaint;
                    IEnumerator enumerator = ListViewGUI.ListView(this.m_ListView, GUIStyle.none, new GUILayoutOption[0]).GetEnumerator();
                    try
                    {
                        while (enumerator.MoveNext())
                        {
                            Texture2D textured;
                            ListViewElement current = (ListViewElement) enumerator.Current;
                            AssetsItem parentAI = this.m_assets[current.row];
                            Rect position = current.position;
                            position = new Rect(position.x + 1f, position.y, position.width - 2f, position.height);
                            int num4 = CountOccurencesOfChar(parentAI.pathName, '/') - this.m_LeastIndent;
                            if (flag && (this.m_ListView.row == current.row))
                            {
                                ms_Constants.ConsoleEntryBackOdd.Draw(position, false, false, true, false);
                            }
                            int enabled = parentAI.enabled;
                            parentAI.enabled = !GUI.Toggle(new Rect(position.x + 3f, position.y, 16f, 16f), parentAI.enabled != 0, string.Empty) ? 0 : 1;
                            position = new Rect(position.x + 3f, position.y, position.width - 3f, position.height);
                            if (GUI.Toggle(position, false, string.Empty, GUIStyle.none) || (enabled != parentAI.enabled))
                            {
                                this.m_ListView.row = current.row;
                                GUIUtility.keyboardControl = this.m_ListView.ID;
                                this.CheckChildren(parentAI);
                            }
                            Rect rect2 = new Rect(position.x + (15 * num4), position.y + 1f, 16f, 16f);
                            Texture2D icon = this.GetIcon(parentAI);
                            if (icon != null)
                            {
                                textured = icon;
                            }
                            else
                            {
                                textured = EditorGUIUtility.FindTexture("DefaultAsset Icon");
                            }
                            if (textured != null)
                            {
                                GUI.DrawTexture(rect2, textured);
                            }
                            position = new Rect((position.x + 20f) + (15 * num4), position.y, position.width - (20 + (15 * num4)), position.height);
                            GUI.Label(position, parentAI.message);
                            if (parentAI.exists == 0)
                            {
                                Texture image = ASMainWindow.badgeNew.image;
                                GUI.DrawTexture(new Rect((current.position.width - image.width) - 6f, current.position.y + ((current.position.height - image.height) / 2f), (float) image.width, (float) image.height), image);
                            }
                        }
                    }
                    finally
                    {
                        IDisposable disposable = enumerator as IDisposable;
                        if (disposable == null)
                        {
                        }
                        disposable.Dispose();
                    }
                    this.FrameLastGUIRect();
                    GUILayout.EndVertical();
                    if (this.m_PackageContainsPreviews)
                    {
                        GUILayout.Space(3f);
                        string packageIconPath = this.m_PackageIconPath;
                        if (((this.m_ListView.row >= 0) && (this.m_ListView.row < this.m_assets.Length)) && (this.m_assets[this.m_ListView.row].previewPath != string.Empty))
                        {
                            packageIconPath = this.m_assets[this.m_ListView.row].previewPath;
                        }
                        GUILayoutOption[] optionArray1 = new GUILayoutOption[] { GUILayout.Width(128f) };
                        GUILayout.BeginVertical(optionArray1);
                        GUILayout.Label("Preview", ms_Constants.title, new GUILayoutOption[0]);
                        GUILayout.Space(1f);
                        this.ShowPreview(packageIconPath);
                        GUILayout.EndVertical();
                        GUILayout.Space(3f);
                        GUILayout.EndHorizontal();
                    }
                    if (((this.m_ListView.row != -1) && (GUIUtility.keyboardControl == this.m_ListView.ID)) && ((Event.current.type == EventType.KeyDown) && (Event.current.keyCode == KeyCode.Space)))
                    {
                        this.m_assets[this.m_ListView.row].enabled = (this.m_assets[this.m_ListView.row].enabled != 0) ? 0 : 1;
                        this.CheckChildren(this.m_assets[this.m_ListView.row]);
                        Event.current.Use();
                    }
                    GUILayout.Space(5f);
                    GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                    GUILayout.Space(10f);
                    GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(50f) };
                    if (GUILayout.Button(EditorGUIUtility.TextContent("All"), options))
                    {
                        for (int j = 0; j < this.m_assets.Length; j++)
                        {
                            this.m_assets[j].enabled = 1;
                        }
                    }
                    GUILayoutOption[] optionArray3 = new GUILayoutOption[] { GUILayout.Width(50f) };
                    if (GUILayout.Button(EditorGUIUtility.TextContent("None"), optionArray3))
                    {
                        for (int k = 0; k < this.m_assets.Length; k++)
                        {
                            this.m_assets[k].enabled = 0;
                        }
                    }
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button(EditorGUIUtility.TextContent("Cancel"), new GUILayoutOption[0]))
                    {
                        base.Close();
                        GUIUtility.ExitGUI();
                    }
                    if (GUILayout.Button(EditorGUIUtility.TextContent("Import"), new GUILayoutOption[0]))
                    {
                        if (this.m_assets != null)
                        {
                            AssetServer.ImportPackageStep2(this.m_assets);
                        }
                        base.Close();
                        GUIUtility.ExitGUI();
                    }
                    GUILayout.Space(10f);
                    GUILayout.EndHorizontal();
                    GUILayout.Space(10f);
                }
                else
                {
                    GUILayout.Label("Nothing to import!", EditorStyles.boldLabel, new GUILayoutOption[0]);
                    GUILayout.Label("All assets from this package are already in your project.", "WordWrappedLabel", new GUILayoutOption[0]);
                    GUILayout.FlexibleSpace();
                    GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button("OK", new GUILayoutOption[0]))
                    {
                        base.Close();
                        GUIUtility.ExitGUI();
                    }
                    GUILayout.EndHorizontal();
                }
            }
        }

        public static void ShowImportPackage(AssetsItem[] items, string packageIconPath)
        {
            PackageImport window = EditorWindow.GetWindow<PackageImport>(true, "Importing package");
            window.m_assets = items;
            window.m_PackageIconPath = packageIconPath;
            if (packageIconPath != string.Empty)
            {
                window.m_PackageContainsPreviews = true;
            }
            else
            {
                foreach (AssetsItem item in items)
                {
                    if (item.previewPath != string.Empty)
                    {
                        window.m_PackageContainsPreviews = true;
                        break;
                    }
                }
            }
            window.Repaint();
        }

        private void ShowPreview(string previewPath)
        {
            if (previewPath != this.m_LastPreviewPath)
            {
                if (this.m_Preview == null)
                {
                    this.m_Preview = new Texture2D(0x80, 0x80);
                }
                byte[] data = null;
                try
                {
                    data = File.ReadAllBytes(previewPath);
                }
                catch
                {
                }
                if (((previewPath == string.Empty) || (data == null)) || !this.m_Preview.LoadImage(data))
                {
                    Color[] pixels = this.m_Preview.GetPixels();
                    for (int i = 0; i < pixels.Length; i++)
                    {
                        pixels[i] = new Color(1f, 1f, 1f, 0f);
                    }
                    this.m_Preview.SetPixels(pixels);
                    this.m_Preview.Apply();
                }
                this.m_LastPreviewPath = previewPath;
            }
            Texture2D image = (this.m_Preview == null) ? EditorGUIUtility.FindTexture("DefaultAsset Icon") : this.m_Preview;
            if (image != null)
            {
                float width = image.width;
                float height = image.height;
                if ((width >= height) && (width > 128f))
                {
                    height = (height * 128f) / width;
                    width = 128f;
                }
                else if ((height > width) && (height > 128f))
                {
                    width = (width * 128f) / height;
                    height = 128f;
                }
                GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandWidth(false) };
                GUI.DrawTexture(GUILayoutUtility.GetRect(width, height, options), image, ScaleMode.ScaleToFit, true);
            }
            this.FrameLastGUIRect();
        }

        internal class Constants
        {
            public GUIStyle ConsoleEntryBackEven = "CN EntryBackEven";
            public GUIStyle ConsoleEntryBackOdd = "CN EntryBackOdd";
            public Color lineColor = ((EditorGUIUtility.skinIndex == 0) ? new Color(0.4f, 0.4f, 0.4f) : new Color(0.1f, 0.1f, 0.1f));
            public GUIStyle title = "OL Title";
        }
    }
}

